<template>
  <div>
    <!-- 搜索 -->
    <!-- 紧急返件入出库管理 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'records'"
      :fetchapi="fetchApi"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isSelectColumn="false"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleDetail" :title="'紧急返件出库'" destroyOnClose width="70%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DetailForm @close="closeDetail" @save="saveDetail" :rowData="rowData"></DetailForm>
    </Drawer>
    <Drawer :visible.sync="visibleEdit" :title="'紧急返件入库'" destroyOnClose width="70%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <EditForm @close="closeEdit" @save="saveEdit" :rowData="rowData"></EditForm>
    </Drawer>
    <Drawer :visible.sync="visibleEditDetail" :title="'紧急返件入库'" destroyOnClose width="70%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <EditMoedlForm @close="closeEditModel" :rowData="rowData"></EditMoedlForm>
    </Drawer>
    <BaseDialog :visible.sync="visible" :title="'车型选择'" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ModelVehicle @close="close" @save="save"></ModelVehicle>
    </BaseDialog>
    <BaseDialog :visible.sync="visibleAdd" :title="'选择经销商'" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="closeAdd" @save="saveAdd"></DealerList>
    </BaseDialog>
  </div>
</template>
<script>
import DetailForm from './DetailForm.vue';
import EditForm from './EditForm.vue';
import EditMoedlForm from './EditMoedlForm.vue';
import ModelVehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import { exportExcelFile } from '@/utils/index';
import { getListByPage, getListForExportOutbound,exporturl} from '@/api/claim/factory/QualityInformationManagement_OEM/CoreIssueRules';
import { getPartType } from '@/api/dictionary/index';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { getAppId } from '@/assets/js/auth';

export default {
  mixins: [dictionary],
  components: {
    DetailForm,
    EditForm,
    ModelVehicle,
    EditMoedlForm,
    DealerList
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: true,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchApi: getListByPage,
      fetchParams: {
        flag: 3,
        xhrAbort: true
      }, //ipa 接口
      selectedRows: [], //选中经销商
      visibleDetail: false,
      visibleEdit: false,
      rowData: {},
      visibleEditDetail: false,
      visibleAdd: false
    };
  },
  created() {
    this.fetchParams.beginInDate = moment()
      .startOf('week')
      .format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endInDate = moment()
      .endOf('week')
      .format('YYYY-MM-DD HH:mm:ss');
    this.chuqi();
  },
  methods: {
    async chuqi() {
      // console.log("进来了")
      // const res1 = await getPartType({type: 5564});
      // if (res1.resultCode == 200) {
      //   this.topFilterInit.find(x => x.fieldName == 'dataSource').itemList = res1.data.map(y => ({
      //     text: y.codeCnDesc,
      //     value: y.codeId
      //   }));
      // }
      // const res2 = await getPartType({type: 5565});
      // if (res2.resultCode == 200) {
      //   delete res2.data[0]
      //   delete res2.data[1]
      //   delete res2.data[3]
      //   delete res2.data[5]
      //   delete res2.data[8]
      //   this.topFilterInit.find(x => x.fieldName == 'urgentStatus').itemList = res2.data.map(y => ({
      //     text: y.codeCnDesc,
      //     value: y.codeId
      //   }));
      // }
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    modelTurnOn() {
      this.visible = true;
    },
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000165' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == 'beginLssuedDate|endLssuedDate').initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == 'beginLssuedDate|endLssuedDate').initialValue[1];
      // if (!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '下发开始日期不可为空！' });
      //   return;
      // }
      // if (!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '下发结束日期不可为空！' });
      //   return;
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '入库日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '入库日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, { flag: 3, xhrAbort: false });
      // obj.beginCreatedAt = this.topFilterInit.find(x => x.fieldName == 'beginInDate|endInDate').initialValue[0];
      // obj.endCreatedAt = this.topFilterInit.find(x => x.fieldName == 'beginInDate|endInDate').initialValue[1];
      // getListForExportOutbound(obj).then(res => {
      //   exportExcelFile(res.data, '紧急返件入出库信息');
      // });
    },
    detail(val) {
      this.rowData = val;
      this.visibleDetail = true;
    },
    edit(val) {
      this.rowData = val;
      this.visibleEdit = true;
    },
    detailModel(val) {
      this.rowData = val;
      this.visibleEditDetail = true;
    },
    closeDetail() {
      this.visibleDetail = false;
    },
    saveDetail() {
      this.visibleDetail = false;
      this.fetchParams = { ...this.fetchParams };
    },
    closeEdit() {
      this.visibleEdit = false;
    },
    closeEditModel() {
      this.visibleEditDetail = false;
    },
    saveEdit() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    },
    close() {
      this.visible = false;
    },
    save(obj) {
      this.visible = false;
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].address;
      this.fetchParams = { ...this.fetchParams };
    },
    chooseDealerNo() {
      this.visibleAdd = true;
    },
    saveAdd(val) {
      this.topFilterInit.find(x => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.visibleAdd = false;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '选择经销商',
          fieldName: 'ownerCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseDealerNo()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: '紧急返件单号', fieldName: 'urgentNo', placeholder: '请输入', initialValue: '' },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSource',
          itemList: this.createDictList('5564'),
          readonly: true // 只读
        },
        {
          type: 'SELECT',
          label: '紧急返件单状态',
          fieldName: 'urgentStatus',
          itemList: this.createDictList('5565', [55651002,55651004, 55651006, 55651009]),
          readonly: true // 只读
        },
        { type: 'INPUT', label: '索赔单号', fieldName: 'cmsClaimNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '质量报告单号', fieldName: 'reportNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '维修工单号', fieldName: 'roNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '旧件名称', fieldName: 'partCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '旧件代码', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          readonly: true, // 只读
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.modelTurnOn()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入', initialValue: '' },
        {
          type: 'RANGE_DATE',
          label: '入库日期',
          fieldName: 'beginInDate|endInDate',
          placeholder: '入库日期',
          dateType: 'daterange',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ]
          // rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        {
          type: 'RANGE_DATE',
          label: '出库日期',
          fieldName: 'beginOutDate|endOutDate',
          placeholder: '出库日期',
          dateType: 'daterange'
        },
        // {
        //   type: 'RANGE_DATE',
        //   label: '下发日期',
        //   fieldName: 'beginLssuedDate|endLssuedDate',
        //   placeholder: '请输入',
        //   initialValue: [
        //     moment()
        //       .startOf('week')
        //       .format('YYYY-MM-DD HH:mm:ss'),
        //     moment()
        //       .endOf('week')
        //       .format('YYYY-MM-DD HH:mm:ss')
        //   ],
        //   rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        // }
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      // if (!val.beginLssuedDate) {
      //   this.$notify.warning({ title: '提示信息', message: '下发开始日期不可为空！' });
      //   return;
      // }
      // if (!val.endLssuedDate) {
      //   this.$notify.warning({ title: '提示信息', message: '下发结束日期不可为空！' });
      //   return;
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(val.endInDate).format('YYYY-MM-DD')).diff(moment(val.beginInDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '入库日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(val.endInDate).format('YYYY-MM-DD')).diff(moment(val.beginInDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '入库日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      this.fetchParams = Object.assign({}, val, {
        flag: 3,
        xhrAbort: false
      });
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 130,
          fixed: 'left',
          render: props => {
            return (
              <div style="text-align:center">
                <el-button size="mini" type="text" onClick={() => this.detailModel(props.row)}>
                  详情
                </el-button>
              </div>
            );
          }
        },
        { title: '紧急返件单状态', dataIndex: 'statusStr', sorter: true },
        { title: '数据来源', dataIndex: 'dataSourceStr', sorter: true },
        { title: '经销商代码', dataIndex: 'dealerCode', sorter: true },
        { title: '经销商名称', dataIndex: 'dealerName', sorter: true },
        { title: '紧急返件单号', dataIndex: 'urgentNo', sorter: true },
        { title: '质量报告单号', dataIndex: 'reportNo', sorter: true },
        { title: '索赔单号', dataIndex: 'cmsClaimNo', sorter: true },
        { title: '维修工单号', dataIndex: 'roNo', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
        { title: '下发日期', dataIndex: 'lssuedDate', sorter: true, dateFormat: 'YYYY-MM-DD' },
        { title: '回运日期', dataIndex: 'expressDate', sorter: true, dateFormat: 'YYYY-MM-DD' },
        { title: '已回运时间', dataIndex: 'diffDate', sorter: true },
        { title: '物流公司', dataIndex: 'expressName', sorter: true },
        { title: '物流单号', dataIndex: 'expressNo', sorter: true },
        { title: '邮寄人', dataIndex: 'expressUser', sorter: true },
        { title: '邮寄人电话', dataIndex: 'expressTel', sorter: true },
        { title: '入库日期', dataIndex: 'inDate', sorter: true, dateFormat: 'YYYY-MM-DD' },
        { title: '出库日期', dataIndex: 'outDate', sorter: true, dateFormat: 'YYYY-MM-DD' }
      ];
    }
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
